package com.neusoft.lohas.modules.sys.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.neusoft.lohas.common.config.Global;
import com.neusoft.lohas.common.persistence.Page;
import com.neusoft.lohas.common.web.BaseController;
import com.neusoft.lohas.modules.ivm.entity.merchant.TimeSlot;
import com.neusoft.lohas.modules.ivm.service.merchant.TimeSlotService;

/**
 * 系统收货时间设置
 * @author jackie.liu
 * @version 2015-01-15
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/slot")
public class SlotController extends BaseController{

	@Autowired
	private TimeSlotService timeSlotService;
	
	@ModelAttribute
	public TimeSlot get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return timeSlotService.get(id);
		}else{
			return new TimeSlot();
		}
	}
	
	@RequiresPermissions("sys:slot:view")
	@RequestMapping(value = {"list", ""})
	public String list(TimeSlot timeSlot, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<TimeSlot> page = timeSlotService.find(new Page<TimeSlot>(request, response), timeSlot); 
        model.addAttribute("page", page);
		return "modules/sys/slotList";
	}

	@RequiresPermissions("sys:slot:view")
	@RequestMapping(value = "form")
	public String form(TimeSlot timeSlot, Model model) {
		model.addAttribute("timeSlot", timeSlot);
		return "modules/sys/slotForm";
	}

	@RequiresPermissions("sys:slot:edit")
	@RequestMapping(value = "save")//@Valid 
	public String save(TimeSlot timeSlot, HttpServletRequest request, Model model, RedirectAttributes redirectAttributes) {
		if(Global.isDemoMode()){
			addMessage(redirectAttributes, "演示模式，不允许操作！");
			return "redirect:"+Global.getAdminPath()+"/sys/slot/?repage";
		}
		if (!beanValidator(model, timeSlot)){
			return form(timeSlot, model);
		}
		timeSlotService.save(timeSlot);
		addMessage(redirectAttributes, "保存收货时间成功");
		return "redirect:"+Global.getAdminPath()+"/sys/slot/?repage";
	}
	
	@RequiresPermissions("sys:slot:edit")
	@RequestMapping(value = "delete")
	public String delete(String id, RedirectAttributes redirectAttributes) {
		if(Global.isDemoMode()){
			addMessage(redirectAttributes, "演示模式，不允许操作！");
			return "redirect:"+Global.getAdminPath()+"/sys/slot/?repage";
		}
		timeSlotService.delete(id);
		addMessage(redirectAttributes, "删除收货时间成功");
		return "redirect:"+Global.getAdminPath()+"/sys/slot/?repage";
	}
	
	/**
	 * 批量修改单位排序
	 */
	@RequiresPermissions("sys:slot:edit")
	@RequestMapping(value = "updateSort")
	public String updateSort(String[] ids, Integer[] sorts, RedirectAttributes redirectAttributes) {
		if(Global.isDemoMode()){
			addMessage(redirectAttributes, "演示模式，不允许操作！");
			return "redirect:"+Global.getAdminPath()+"/sys/slot/?repage";
		}
    	int len = ids.length;
    	TimeSlot[] slots = new TimeSlot[len];
    	for (int i = 0; i < len; i++) {
    		slots[i] = timeSlotService.get(ids[i]);
    		slots[i].setSort(sorts[i]);
    		timeSlotService.save(slots[i]);
    	}
    	addMessage(redirectAttributes, "保存收货时间排序成功!");
		return "redirect:"+Global.getAdminPath()+"/sys/slot/?repage";
	}
}
